package com.dream.testtubebaby_doctor;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.dream.testtubebaby_doctor.adapter.ProductListBaseAdapter;
import com.dream.testtubebaby_doctor.utils.AppUtil;
import com.dream.testtubebaby_doctor.utils.ToastUtil;
import com.dream.testtubebaby_doctor.view.xlistview.IXListViewLoadMore;
import com.dream.testtubebaby_doctor.view.xlistview.IXListViewRefreshListener;
import com.dream.testtubebaby_doctor.view.xlistview.XListView;

public abstract class ProductListBaseActivity extends AppCompatActivity implements
		IXListViewRefreshListener, IXListViewLoadMore {
	protected static final int LOAD_MORE = 0x110;
	protected static final int LOAD_REFREASH = 0x111;
	public static final int TIP_ERROR_NO_NETWORK = 0X112;
	public static final int TIP_ERROR_SERVER = 0X113;
	protected static final int HANDLER_REFREASH = 0x114;
	protected static final int UPDATE_REFREASH_OK = 0x115;
	protected static final int MORE_REFREASH_OK = 0x116;
	protected static final int download_step = 10;
	protected long fromid = 0;

	protected XListView product_listview;
	protected ProductListBaseAdapter<?> mProductListAdapter;
	protected Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HANDLER_REFREASH:
				mProductListAdapter.notifyDataSetChanged();
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_list_view);
		initView();
		initXlistView();
	}

	private void initView() {
		ImageView top_back_img = (ImageView)findViewById(R.id.top_back_img);
		top_back_img.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	protected abstract ProductListBaseAdapter<?> getAdapter();
	protected abstract void updateAdapter();
	protected abstract void moreUpdateAdapter();
	
	private void initXlistView() {
		product_listview = (XListView) findViewById(R.id.product_listview);
		mProductListAdapter = getAdapter();
		product_listview.setAdapter(mProductListAdapter);
		product_listview.setPullRefreshEnable(this);
		product_listview.setPullLoadEnable(this);
		product_listview.setRefreshTime(AppUtil.getRefreashTime(this, 0));
		product_listview.startRefresh();
	}

	@Override
	public void onLoadMore() {
		new LoadDatasTask().execute(LOAD_MORE);
	}

	@Override
	public void onRefresh() {
		new LoadDatasTask().execute(LOAD_REFREASH);
	}

	class LoadDatasTask extends AsyncTask<Integer, Void, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			switch (params[0]) {
			case LOAD_MORE:
				return loadMoreData();
			case LOAD_REFREASH:
				return refreashData();
			}
			return -1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			switch (result) {
			case TIP_ERROR_NO_NETWORK:
				ToastUtil.toast(ProductListBaseActivity.this,
						R.string.newwork_connect_error);
				break;
			case TIP_ERROR_SERVER:
				ToastUtil.toast(ProductListBaseActivity.this,
						R.string.server_error);
				break;
			case UPDATE_REFREASH_OK:
				updateAdapter();
				break;
			case MORE_REFREASH_OK:
				moreUpdateAdapter();
				break;
			default:
				break;

			}
			product_listview.setRefreshTime(AppUtil.getRefreashTime(
					ProductListBaseActivity.this, 0));
			product_listview.stopRefresh();
			product_listview.stopLoadMore();
		}

	}

	protected int loadMoreData() {
		return -1;
	}

	protected int refreashData() {
		return -1;
	}
}
